<?php 
class product_show extends MX_Controller{
	public $category;
	public function index($link){

		$setting=$this->load->Module('setting/data');
		
		$annonce=false;
		$id=$this->model()->get_id_from_link($link);
		if($id) {
			$annonce=self::get_info($id);
			//update count nombre de vue
			$this->load->library('session');
			$prd_counter=$this->session->userdata('prd_counter');
			if(!is_array($prd_counter) or (is_array($prd_counter) and !in_array($id,$prd_counter))) $nbVue=$this->model()->counter($id);
			$prd_counter[]=$id;
			$prd_counter=array_unique($prd_counter);
			//set session region
			$this->session->set_userdata('prd_counter',$prd_counter);
		}

		//si annonce est non valide
		if(!$annonce){
			
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
					$data); // Display the page
			return false;
		}
		
		$title=$annonce->prd_name;
		$description=self::truncate(strip_tags($annonce->prd_description),150);
		$keywords=$annonce->prd_keywords;
		//list tous annonce payant
		$TopAnnonces=array();
		$product=$this->load->Module('product/show');
		$limit=4;
		$annoncesList=$this->model('category_models_show')->get_TopAnnonce($annonce->category_id,$limit);
		if(count($annoncesList))
		foreach ($annoncesList as $id_annonce){
			$TopAnnonces[$id_annonce]=$product->get_info($id_annonce);
		}
		
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'TopAnnonces'=>$TopAnnonces,
				'filter'=>'',
				'annonce'=>$annonce
		);
		
		$data=$setting->data($data);
		
		
		$this->view(
				array('header',
						'annonce',						
						'footer'),
						$data); // Display the page
		
	}
	public function get_info($id,$active=true){
		$annonce=$this->model('product_models_admin')->get_info_product($id);
		//check active
		if($active){
			if(!$annonce)return false;
			if(isset($annonce->prd_active) and !$annonce->prd_active) return false;
		}
		
		//convertir date
		setlocale(LC_ALL, "fr_BE");
		$date=date('d/m/Y',$annonce->date_validate);
		if($date==date('d/m/Y')) $date="Aujourd'hui";
		if($date==date('d/m/Y',strtotime('yesterday'))) $date="Hier";
		$date.=' '.date(' H:i',$annonce->date_validate);
		$annonce->date=$date;
		
		//translate codepostal -> ville
		$query = $this->db->get_where('departement_ville', array('Cp' => $annonce->prd_codepostal), 1);
		$row= $query->result();
		if(!empty($row))
			$annonce->ville=$row[0]->N_Com_min;
		
		$category=$this->load->module('category/admin');
		$annonce->category_name=$category->get_name_category($annonce->category_id);
		$annonce->link_category=$category->get_link_category($annonce->category_id);
		$annonce->is_rencontres=$this->model('category_models_show')->is_rencontres($annonce->category_id);
		
		$annonce->breadcrumb_category=$category->get_breadcrumb_category($annonce->category_id);
		$annonce->html_category=$category->get_html_category($annonce->category_id);
		
		//annonce option
		$annonce->option_label=array();
		$annonce->option_unit=array();
		if(isset($annonce->option) and !empty($annonce->option)){
			$query = $this->db->select('*')->from('options')->where_in( 'id_type',array_keys($annonce->option))->get();
			foreach ($query->result() as $row){
				$explode=explode(' - ',$row->name);
				$label=isset($explode[1])?$explode[1]:$row->name;
				$annonce->option_label[$row->id_type]=$label;
				$annonce->option_unit[$row->id_type]=$row->unit;
			}
		
		}else $annonce->option=array();
		
		return $annonce;
	}
	private function truncate($string, $limit, $break=" ", $pad="") { 
		// return with no change if string is shorter than $limit 
		if(strlen($string) <= $limit) 
			return $string; 
		$string = substr($string, 0, $limit); 
		if(false !== ($breakpoint = strrpos($string, $break)))
			$string = substr($string, 0, $breakpoint); 
		return $string . $pad; 
	}
}